package org.longteng.mobileluck.algorithm.bean;

import com.univocity.parsers.annotations.Parsed;
import org.apache.commons.lang3.StringUtils;
import org.longteng.core.utils.ChineseYijingNumUtil;
import org.longteng.core.utils.StringExUtils;

import java.util.stream.Stream;

/**
 * 手机号码运势表,直接读入csv表src/main/resources/table/mobilenum_fortune.csv
 * @author hymn.com
 */
public class MobileNumFortuneTable extends MobileNumAuspiciousTable {

    /** 运势简述状态*/
    @Parsed(field = "简述")
    private String fortuneStatus;

    public String getFortuneStatus() {
        return fortuneStatus;
    }

    public void setFortuneStatus(String fortuneStatus) {
        this.fortuneStatus = fortuneStatus;
    }

    @Override
    public String toString() {
        return "MobileNumFortuneTable{" +
                "mobileNum=" + mobileNum +
                ", meaning='" + meaning + '\'' +              // 五行含意
                ", luckStatus='" + luckStatus + '\'' +        // 单双比,总和卦象
                ", fortuneStatus='" + fortuneStatus + '\'' +  // 三才得配
                ", 完整号='" + mobilePhoneNumber + '\'' +
                '}';
    }

    /**
     * 解释生成车牌号的 五行含意，单双比,总和卦象，三才得配
     * @param cartPlateNum
     * @return
     */
    public static MobileNumFortuneTable parseCartNumFortune(String cartPlateNum) {
        MobileNumFortuneTable mobileNumFortuneTable = new MobileNumFortuneTable();
        mobileNumFortuneTable.setMobilePhoneNumber(cartPlateNum);
        String cartNum = StringUtils.getDigits(cartPlateNum);
        // 五行
        mobileNumFortuneTable.setLuckStatus(ChineseYijingNumUtil.parseWuXingCountDescStr(cartNum));

        char[] cartNumArr = cartNum.toCharArray();
        int[] evenOrOddCount = new int[]{0,0};   // 单双比
        int sum = 0;
        for (char cartNumChar : cartNumArr) {
            int num = Integer.parseInt(String.valueOf(cartNumChar));
            sum += num;
            if ((num & 1) == 0) {
                evenOrOddCount[0]++;
            }else {
                evenOrOddCount[1]++;
            }
        } // for end
        final int lucksum = sum;
        // 卦象无不良:如所有数字的和不要为6、18、14、16、19、22、26、29、32
        boolean lucksumStatus = Stream.of(6,18,14,16,19,22,26,29,32).allMatch(e->e.intValue()==lucksum);
        String meaning = String.format("单双比:%d/%d(%s),卦象:%s", evenOrOddCount[1], evenOrOddCount[0]
                , evenOrOddCount[1] == 0 || evenOrOddCount[0] == 0 ? "凶":"吉"
                ,lucksumStatus ? "凶":"吉");
        mobileNumFortuneTable.setMeaning(meaning+";五行:"+mobileNumFortuneTable.getLuckStatus());
        // 三才得配
        mobileNumFortuneTable.setFortuneStatus(ChineseYijingNumUtil.talent3Str(cartNumArr));
        return mobileNumFortuneTable;
    }

}
